/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import {
  getPlatformList,
  getPlatformBindCode,
  cancelPlatformBind,
  checkScanCodeStatus,
  getQrCodeInfo,
  authPlatform,
  bindYzAccount,
  unBindSubYz,
  getBindInnAccount,
  isCityLeague,
} from "@/services/platform";
import { isLegalData } from "@/utils/utils";
import { isEmpty } from "lodash";

export default {
  namespace: "platform",
  state: {
    data: null,
    qrData: null,
    sceneId: null,
    platformList: {},
    qrCodeInfo: {},
    hasBind: false, // 是否绑定公众号或者小程序
    isCityLeague: true,
    bindYz: false,
  },
  effects: {
    *getPlatformList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPlatformList, payload);
      if (!response) return;
      const { code, data = [], msg } = response;
      const hasBind = isLegalData(data, []).length > 0;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            hasBind,
            platformList: {
              weixin: isLegalData(data, []).filter(val => val.platformType == "weixin")[0],
              weixin_mini: isLegalData(data, []).filter(
                val => val.platformType == "weixin_mini"
              )[0],
            },
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getQrCodeInfo({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getQrCodeInfo, payload);
      if (!response) return;
      const { code, data = [], msg } = response;
      if (code == 0 && data) {
        yield put({
          type: "save",
          payload: {
            qrCodeInfo: {
              weixin: isLegalData(data, []).filter(val => val.platformType == "weixin"),
              weixin_mini: isLegalData(data, []).filter(val => val.platformType == "weixin_mini"),
            },
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    *authPlatform({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(authPlatform, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0 && data) {
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    *getPlatformBindCode({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(getPlatformBindCode, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0 && data) {
        if (!data.url) {
          message.error("暂无二维码！");
          __dva_reject(response);
          return;
        }
        yield put({
          type: "save",
          payload: {
            qrData: data,
          },
        });
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject(response);
      }
    },
    *cancelPlatformBind({ payload }, { call, put }) {
      const response = yield call(cancelPlatformBind, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success("已解除关联");
        // 清除所有数据
        yield put({
          type: "save",
          payload: {
            data: null,
            qrData: null,
            sceneId: null,
          },
        });
      } else {
        message.error(msg);
      }
    },
    *checkScanCodeStatus({ payload, then }, { call }) {
      const response = yield call(checkScanCodeStatus, payload);
      if (!response) return;
      then(response);
    },
    // 绑定驿站账号
    *bindYzAccount({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(bindYzAccount, payload);
      if (!response) return;
      const { code, msg } = response;
      if (code == 0) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
    // 解绑驿站
    *unBindSubYz({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(unBindSubYz, payload);
      if (!response) return;
      const { code, data, msg } = response;
      if (code == 0 && data) {
        message.success(msg);
        __dva_resolve(response);
      } else {
        message.error(msg);
        __dva_reject();
      }
    },
    // 是否是新零售加盟商
    *isCityLeague({ payload, __dva_resolve }, { call, put }) {
      const response = yield call(isCityLeague, payload);
      if (!response) return;
      const { code, msg, data } = response;
      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            isCityLeague: data,
          },
        });
        // 非新零售加盟商
        if (!data) {
          // 获得新零售绑定的驿站信息
          const bindYzInfo = yield call(getBindInnAccount);
          yield put({
            type: "save",
            payload: {
              bindYz: !isEmpty(isLegalData(bindYzInfo.data, {})),
            },
          });
        }
        __dva_resolve(data);
      } else {
        message.error(msg);
      }
    },
  },
  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
